package com.contextroot.watercooler.server;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.contextroot.watercooler.client.ChatService;
import com.contextroot.watercooler.client.Message;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class ChatServiceImpl extends RemoteServiceServlet implements
		ChatService {

	private static final long serialVersionUID = 1L;

	private static List<Message> messages = new ArrayList<Message>();
	
	@Override
	public List<Message> findAll() {
		return messages;
	}
	
	public  void postMessage(final Message message) {
		// TODO Save messages to database
		final User user = UserServiceFactory.getUserService().getCurrentUser();
		message.setUsername(user.getNickname());
		// User server datetime
		message.setDatetime(new Date());
		messages.add(message);
	}
}
